package com.guigui.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.DesignModuleDetails;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.DesignModuleDetailsService;
import com.guigu.erp.service.DesignModuleService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import com.guigui.erp.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Lenovo
 */
@Controller
@RequestMapping("/design/module")
public class DesignModuleController {
    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    DesignModuleDetailsService designModuleDetailsService;

    /**
     * 处理制定物料设计单的确认按钮请求
     */
    @RequestMapping("/add")
    @ResponseBody
    public String addModule(@RequestBody DesignModuleDto designModuleDto) {

        // 生成物料设计单编号
        String designId = numberUtils.getTypeNumber("200");
        designModuleDto.setDesignId(designId);

        // 设置物料总成本
        BigDecimal sum = new BigDecimal(0);
        List<DesignModuleDetails> details = designModuleDto.getDetails();
        for (int i = 0; i < details.size(); i++) {
            sum=sum.add(details.get(i).getSubtotal());
        }
        designModuleDto.setCostPriceSum(sum);

        // 设置审核标志 S001-0 等待审核
        designModuleDto.setCheckTag("S001-0");

        // 设置变更标志 B002-0 未变更
        designModuleDto.setChangeTag("B002-0");

        //设置当前物料设计单的分类编号
        String productId = designModuleDto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<DesignFile>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        if (designFile != null) {
            designModuleDto.setFirstKindId(designFile.getFirstKindId());
            designModuleDto.setFirstKindName(designFile.getFirstKindName());
            designModuleDto.setSecondKindId(designFile.getSecondKindId());
            designModuleDto.setSecondKindName(designFile.getSecondKindName());
            designModuleDto.setThirdKindId(designFile.getThirdKindId());
            designModuleDto.setThirdKindName(designFile.getThirdKindName());
        }
        //调用业务层方法
        designModuleService.makeDesignModule(designModuleDto);
        return "correct";
    }

    /**
     * 物料组成设计查询请求
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageUtils<DesignModule> designModuleList(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignModule> page = this.designModuleService.page(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 物料组成设计复核查询
     */
    @RequestMapping("/designReview2")
    @ResponseBody
    public PageUtils<DesignModule> recheckPage2(DesignFileVo vo, int pageNo, int pageSize) {
        PageUtils<DesignModule> recheckPage2 = this.designModuleService.recheckPage2(vo, pageNo, pageSize);
        return recheckPage2;
    }

    /**
     * 根据物料组成id查询对应的物料组成信息
     */
    @RequestMapping("/designModuleGetId")
    @ResponseBody
    public DesignModule designModuleGetId(int id) {
        DesignModule designModule = this.designModuleService.getById(id);
        return designModule;
    }

    /**
     * 物料组成设计复核通过
     * @param
     * @return
     */
    @RequestMapping("designModuleReviewPassed2")
    @ResponseBody
    public String designModuleReviewPassed2(DesignModule designModule) {
        designModule.setCheckTag("S001-1");
        boolean flag = designModuleService.updateById(designModule);
        if (flag) {
            return "correct";
        }
        return "error";
    }

    /*
     * 修改物料明细*/
    @RequestMapping("/upd_detail")
    @ResponseBody
    public String upd_detail(@RequestBody List<DesignModuleDetails> list){
        for (int i=0;i<list.size();i++){
            DesignModuleDetails details=list.get(i);
            designModuleDetailsService.updateById(details);
        }
        return "correct";
    }
}